<!DOCTYPE html>
<html lang="zh-cn">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>htmlcss 阶段</title>
    <style></style>
</head>

<body>



    <script>
        // 写一个函数 创建一个闭包
        // 调用内部函数，可以返回随机人物名字
        function showName() {
            var nameArray = [
                "曹操",
                "孙权",
                "刘备",
                "关羽",
                "张飞",
                "赵云",
                "黄忠",
                "马超"
            ]
            function sanguo() {
                var len = nameArray.length;
                // console.log(len);
                var res = Math.floor(Math.random() * len);
                // console.log(res);
                for (i = 0; i < len; i++) {
                    if (i == res) {
                        console.log(nameArray[i]);
                    }
                }
            }
            return sanguo();
        }


        // 例如：
        showName();
        // console.log(f);// "赵云"
        // console.log(f());// "关羽"






    </script>
</body>

</html>